using System;
using Core.Extensions;


namespace SolarDevices
{
    public class TriStarLookupTables
    {
        static readonly Tupple[] BatteryStage =
            new[]{
                new Tupple { Description = "(Start) System startup.", Value = 0 },
                new Tupple { Description = "(Night check) No power, detecting nightfall.", Value = 1 },
                new Tupple { Description = "(Disconnected) No power.", Value = 2 },
                new Tupple { Description = "(Night) No power, waiting for power source.", Value = 3 },
                new Tupple { Description = "(Fault) Detected fault.", Value = 4 },
                new Tupple { Description = "(Bulk) Max power point tracking until absorb voltage reached.", Value = 5 },
                new Tupple { Description = "(Absorb) Regulating battery voltage at absorb set point until the batteries are charged.", Value = 6 },
                new Tupple { Description = "(Float) Max power point tracking. Seeking float set point voltage.", Value = 7 },
                new Tupple { Description = "(Equalize) Regulating battery voltage at equalize set point.", Value = 8 },
                new Tupple { Description = "(Slave) State set by master charge controller.", Value = 9 }
            };

        static readonly Tupple[] InfoMap =
            new[]{
                new Tupple { Description = "Over Current", Value = 0x00000001 },
                new Tupple { Description = "FETs shorted", Value = 0x00000002 },
                new Tupple { Description = "software bug", Value = 0x00000004 },
                new Tupple { Description = "battery HVD", Value = 0x00000008 },
                new Tupple { Description = "array HVD", Value = 0x00000010 },
                new Tupple { Description = "RTS shorted", Value = 0x00000080 },
                new Tupple { Description = "RTS Disconnected", Value = 0x00000100 },
                new Tupple { Description = "EEPROM retry limit", Value = 0x00000200 },
                new Tupple { Description = "Slave control timeout", Value = 0x00000800 }
            };

            public static string ParseInfoFlag(UInt32 infoFlag)
            {
                var b = new StringBuilder("");
                bool first = true;
                foreach (var infoItem in InfoMap)
                {
                    var bit = infoFlag & infoItem.Value;
                    if (bit != 0)
                    {
                        if (!first)
                        {
                            b.Append(" - ");
                        }
                        else
                        {
                            first = false;
                        }
                        b.Append(infoItem.Description);
                    }
                }
                return b.ToString();
            }

            public static string LookupBatteryStage(int val)
            {
                foreach (var infoMap in BatteryStage)
                {
                    if (val == infoMap.Value)
                    {
                        return infoMap.Description;
                    }
                }
                return "Undefined";
            }

        }
}
